<?php
include 'room_lib.php';

$a_css_files = array (
    array ( 'path' => 'plugins/bootstrap-modal/css/bootstrap-modal.css' ),
);

$a_js_files = array (
    array ( 'path' => 'plugins/jquery-file-upload/js/vendor/tmpl.min.js' ),
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modal.js' ),
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modalmanager.js' ),
    array ( 'path' => 'scripts/nh-room.js' ),
);

$a_js_class[] = 'NH_Room.init();';

$a_GLV = $a_User_Class = $a_Room = array ( );

// <editor-fold defaultstate="collapsed" desc="Get Data">
$s_Where = 'where course = "' . $a_KhoaHoc_Info['start_year'] . '" ';

$db_query = "
    select   team, level, crew, count(userid) as total
    from     user_class
    $s_Where
    group by team, level, crew
    order by team, level, crew";
$d_total  = db_query_single_value($db_conn, $db_query);

$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)) {
    $a_User_Class["$d_row->team-$d_row->level-$d_row->crew"] = $d_row;
}

if (!empty($a_User_Class)) {
    
    /*
     * Get HT Informations
     */
    $a_tmp_option = array(
        'class_condition'=> "course = '" . $a_KhoaHoc_Info['start_year'] . "'",
        'user_condition' => "usertype = 'HUYNH_TRUONG'",
    );

    $a_tmp_user = get_class_to_user($a_tmp_option, $db_conn);
    
    foreach ($a_tmp_user as $key => $d_row) {
        $a_GLV[$d_row->team][$d_row->level][$d_row->crew][$d_row->userid] = $d_row;
    }
    
    unset($a_tmp_user, $a_tmp_option);
    
    /*
     * Get Room Information
     */
    $db_query = "
        select  roomid, team, level, crew, location, note
        from    room
        order by team, level, crew";

    $db_result = db_query($db_conn, $db_query);

    while ($d_row = db_fetch($db_result)) {
        if ($d_row->note) { $d_row->note = $d_row->note; }

        $a_Room[$d_row->team][$d_row->level][$d_row->crew] = $d_row;
        
        if (!isset($a_User_Class["$d_row->team-$d_row->level-$d_row->crew"])) {
            $a_User_Class["$d_row->team-$d_row->level-$d_row->crew"] = $d_row;
        }
    }
}

ksort($a_User_Class);

$a_permission = get_passport_action_from_module($db_conn, array('PHONG_HOC'));
// </editor-fold>

include 'lib_head.php';
?>
<div class="portlet box green">
    <div class="portlet-title">
        <div class="caption"><i class="icon-list"></i>Vị Trí Phòng Học</div>
    </div>
    <div class="portlet-body">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th style="text-align: center;">Lớp</th>
                    <th style="text-align: center;" class="hidden-phone">Sĩ Số</th>
                    <th style="text-align: center;" class="hidden-phone">Huynh Trưởng</th>
                    <th style="text-align: center;">Vị Trí</th>
                    <th style="text-align: center;" class="hidden-phone">Ghi Chú</th>
                </tr>
            </thead>
            <tbody>
            <?php
            foreach ($a_User_Class as $d_user_class) {

                $v_tmp_name  = isset($a_Nganh   [$d_user_class->team   ]) ?         $a_Nganh[$d_user_class->team ] : '';
                $v_tmp_name .= isset($a_Cap     [$d_user_class->level  ]) ? ' - ' . $a_Cap  [$d_user_class->level] : '';
                $v_tmp_name .= isset($a_Doi     [$d_user_class->crew   ]) ? ' - ' . $a_Doi  [$d_user_class->crew ] : '';
                
                $o_tmp_room = @$a_Room[$d_user_class->team][$d_user_class->level][$d_user_class->crew];
                ?>
                <tr>
                    <td class="set-room" team="<?=$d_user_class->team?>" level="<?=$d_user_class->level?>" crew="<?=$d_user_class->crew?>" location="<?=@$o_tmp_room->location?>" note="<?=@$o_tmp_room->note?>" classname="<?=$v_tmp_name?>">
                        <div><?=$v_tmp_name?></div>
                        <a class="btn mini yellow-stripe user <?=in_array('update-location',$a_permission)?'':'hidden'?>">
                            <i class="icon-map-marker"></i>
                            Cập Nhật
                        </a>
                    </td>
                    <td class="hidden-phone" style="text-align: center;"><?=$d_user_class->total?></td>
                    <td class="hidden-phone">
                        <?php if (isset($a_GLV[$d_user_class->team][$d_user_class->level][$d_user_class->crew])) : foreach ($a_GLV[$d_user_class->team][$d_user_class->level][$d_user_class->crew] as $o_user) : ?>
                        <span><?=$o_user->saint?> <?=$o_user->username?> </span><br/>
                        <?php endforeach; endif; ?>
                    </td>
                    <td class="result"      ><?=@$o_tmp_room->location ?></td>
                    <td class="hidden-phone"><?=@$o_tmp_room->note     ?></td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
</div>
<div id="responsive" class="modal hide fade" tabindex="-1">
</div>
<script id="room-tmpl" type="text/x-tmpl">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h3>{%=o.classname%}</h3>
    </div>
    <div class="modal-body">
        <input type="hidden" name="team" value="{%=o.team%}"> 
        <input type="hidden" name="level" value="{%=o.level%}"> 
        <input type="hidden" name="crew" value="{%=o.crew%}"> 
        <div class="row-fluid">
            <div class="span12 ">
                <div class="control-group">
                    <label class="control-label">Vị Trí</label>
                    <div class="controls">
                        <input type="text" name="location" value="{%=o.location%}" class="m-wrap span12" placeholder="Phòng A2" >
                    </div>
                </div>
            </div>
        </div>
        <div class="row-fluid">
            <div class="span12 ">
                <div class="control-group">
                    <label class="control-label">Ghi Chú</label>
                    <div class="controls">
                        <textarea class="span12 m-wrap" name="note">{%=o.note%}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn">Thoát</button>
        <button type="button" class="btn blue btn-save">Lưu</button>
    </div>
</script>
<?php include 'lib_tail.php'; ?>